[% setvar title Lexical variables made default %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Lexical variables made default</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: J. David Blackstone &lt;<a href='mailto:jdavidb@dfw.net'>jdavidb@dfw.net</a>&gt;
  Date: 1 Aug 2000
  Last Modified: 26 Sep 2000
  Mailing List: <a href='mailto:perl6-language-strict@perl.org'>perl6-language-strict@perl.org</a>
  Number: 6
  Version: 3
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Prior to version 5, all implementations of Perl were designed with
only dynamic (global) variables in mind.  Perl5 provided lexical
variables in a backward compatible way, along with a pragma to force
either declaration of variables or import of dynamic variables.  Perl6
should make lexical variables and required variable declarations the
default.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<p>The author cannot decide whether doing this with something analogous
to a <code>use strict 'vars'</code> is better or worse than doing this with a
proposal like RFC 106.  Although the RFC 106 proposal seemed to gather
more interest, there were still enough naysayers that I believe it
would be incorrect to claim a consensus (as to which, if <b>either</b>,
proposal should be implemented).</p>
<p>These two RFCs should be read in comparison with each other.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Dynamically-scoped variables can have many effects which are
counterintuitive and undesirable.  In Perl5, lexically-scoped
variables are available with the <code>my</code> operator.  Without <code>my</code>, a
variable is assumed to be dynamic.</p>
<p>Perl5 provides the <code>strict</code> pragma, which forces the programmer to do
one of three things: 1) declare a variable to be lexical with <code>my</code>;
2) declare a variable to be dynamic with <code>our</code>; 3) import a dynamic
variable from another namespace with <code>vars</code> or otherwise.  This
forced declaration of variables is generally desirable as good
programming practice, although the ability does exist to turn off the
pragma when necessary.</p>
<p>There are very few circumstances in which dynamic variables have a
distinct advantage over lexicals.  Most modules in the standard
library are deliberately written with <code>strict 'vars'</code> to avoid
interfering with other modules or the application programmer's code.
More recent and enlightened documentation and educational material
teaches the use of lexical variables as preferred.</p>
<p>In Perl6, the concept of <code>strict 'vars'</code> should on by default.  Every
variable should be declared either as lexical with <code>my</code> or dynamic
with <code>our</code>.  New Perl6 programmers should be immediately introduced
to lexical scoping and its benefits.  The ability to disable the
strictness should be retained through <code>no strict 'vars';</code> or an
equivalent, for &quot;quick-and-dirty&quot; programs, backward compatibility,
and where otherwise necessary or desired by those who Know What They
Are Doing.</p>
<p>Attempts to silently make all undeclared variables lexical does not
address the benefits to be gained by forcing declaration of all
variables.  Declarations provide quite a bit of information to the
compiler, including a notion of exactly which block a variable should
be scoped in.  Declarations are also now being used in various
proposals involving strong(er?) typing, constants, O-O, and so on.</p>
<p>This RFC is not suggesting that any of the other features of the
<code>strict</code> pragma be on by default (such as <code>'refs'</code> or <code>'subs'</code>).
If that is desired, it should be submitted in a separate RFC.</p>
<p>The <code>local</code> operator is misnamed, since it operates on dynamic
(global) variables.  This operator should be removed or renamed, but
this should be the subject of a separate RFC.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>I don't know enough about internals to say anything about
implementation from that standpoint.</p>
<p>The following program examples show how declarations would work.</p>
<pre>	my $name = &quot;J. David&quot;;   # lexical variable
	our($VERSION) = 5.31;    # dynamic variable in current package
	$this *= 7;              # illegal; variables must be
	# declared lexical or dynamic
	my($arg1, $arg2) = @_;   # lexical (traditional for a subroutine)
	{
	no strict 'vars';       # or something new
	$quick = 7;             # legal in this scope =
	# dynamic variable in current package
	$quick *= $main::var;
	print &quot;$quick\n&quot;;
	}
	$package::count++;       # always legal (I presume?)</pre>
<a name='Alternative'></a><h2>Alternative</h2>
<p>Although the implementation above is currently considered to be the
best, an alternative would allow undeclared lexical variables.  In
this approach, <code>strict 'vars'</code> would become <code>strict 'decs'</code>,
requiring declaration of all variables.  Without <code>strict 'decs'</code>,
undeclared variables would be considered to be lexical, or variables
could be declared dynamic with <code>our</code> or some other syntax.</p>
<p>RFC 106 gives more details on such an alternative implementation.
This approach would require more complexity in Perl5 -&gt; Perl6
translation and be a much larger step in a new direction for the
language.  Since the state-of-the-practice of Perl5 programming has
evolved to generally include <code>strict 'vars'</code>, it makes more sense to
simply make that the default than to modify the language any further.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 106: Yet another lexical variable proposal: lexical variables made default without requiring strict 'vars'</p>
<p>The section on &quot;Private Variables via my()&quot; in the perlsub manpage.</p>
<p>The Camel book.</p>
</div>
